package com.tianma315.admin.check.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.check.vo.KeepSampleVO;
import com.tianma315.admin.common.base.CoreServiceImpl;
import com.tianma315.admin.sys.domain.UserDO;
import com.tianma315.admin.sys.service.UserService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tianma315.admin.check.dao.KeepSampleDao;
import com.tianma315.admin.check.domain.KeepSampleDO;
import com.tianma315.admin.check.service.KeepSampleService;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.List;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2019-07-08 16:57:00 | wen</small>
 */
@Service
public class KeepSampleServiceImpl extends CoreServiceImpl<KeepSampleDao, KeepSampleDO> implements KeepSampleService {


    @Autowired
    private KeepSampleDao keepSampleDao;

    @Autowired
    private UserService userService;

    @Override
    public Page<KeepSampleDO> keepSamplePage(Integer pageNumber, Integer pageSize, KeepSampleDO keepSampleDTO) {
        // 查询列表数据
        Page<KeepSampleDO> page = new Page<>(pageNumber, pageSize);

//        Wrapper<KeepSampleDO> wrapper = new EntityWrapper<KeepSampleDO>(keepSampleDTO);
//        wrapper.eq("state",0);
//        page = selectPage(page, wrapper);
//        int total = selectCount(wrapper);
        List list = keepSampleDao.getKeepSampleVOPage(page,keepSampleDTO);
        page.setRecords(list);
//        page.setTotal(total);
        return page;
    }

    @Override
    public boolean addKeepSample(KeepSampleDO keepSampleDO) {
        keepSampleDO.setCreateDate(Calendar.getInstance().getTime());
        keepSampleDO.setState(0);
        Boolean result = insert(keepSampleDO);
        return result;
    }

    @Override
    public boolean deleteKeepSample(Integer id) {
        KeepSampleDO keepSampleDO = new KeepSampleDO();
        keepSampleDO.setKeepSampleId(id);
        keepSampleDO.setState(1);
        Boolean result = updateById(keepSampleDO);
        return result;
    }

    @Override
    public List<KeepSampleVO> getKeepSampleByBatchAndProductId(String batch, Long productId) {
        EntityWrapper<KeepSampleDO> wrapper = new EntityWrapper<>();
        wrapper.eq("code",batch);
        wrapper.eq("product_id",productId);
        wrapper.eq("state",0);


        List<KeepSampleDO> keepSampleDOList = selectList(wrapper);

        List<KeepSampleVO> keepSampleVOList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(keepSampleDOList)){
            for (KeepSampleDO keepSampleDO:keepSampleDOList) {
                KeepSampleVO keepSampleVO = new KeepSampleVO();
                BeanUtils.copyProperties(keepSampleDO,keepSampleVO);
                UserDO userDo =userService.selectById(keepSampleDO.getKeepSampleMan());
                keepSampleVO.setName(userDo.getUsername());
                keepSampleVOList.add(keepSampleVO);
            }
        }

        return keepSampleVOList;
    }
}
